package com.sgb.member.ui.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.sgb.member.MemberConfig
import com.sgb.member.R
import com.sgb.member.databinding.ActivityChooseMemberPriceBinding
import com.sgb.member.entity.MemberBean2
import com.sgb.member.ui.adapter.SelectMembarPriceAdapter
import com.sgb.member.view_model.MemberChooseMemberPriceModel
import com.swgk.core.base.AppActivity
import com.swgk.core.dialog.DialogHelper
import com.swgk.core.util.MToast
import java.util.*
import kotlin.collections.ArrayList

/*
调价会员列表
*/
class SelectMemberPriceActivity : AppActivity(), View.OnClickListener {
    private lateinit var viewModel: MemberChooseMemberPriceModel
    private lateinit var mBinding: ActivityChooseMemberPriceBinding
    lateinit var adapter: SelectMembarPriceAdapter
    var data: MutableList<MemberBean2> = ArrayList()
    var members: MutableList<String>? = null
    var isAll = false
    var keyWord = ""
    var comType = ""

    companion object {

        fun jumptToAct(activity: Activity, memberlist: ArrayList<String>) {
            val intent = Intent(activity, SelectMemberPriceActivity::class.java).apply {
                val bundle = Bundle()
                bundle.putStringArrayList("list", memberlist)
                putExtras(bundle)
            }
            activity.startActivityForResult(intent, 11)
        }
    }

    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_choose_member_price)
        mBinding.lifecycleOwner = this
        viewModel = ViewModelProvider(this).get(MemberChooseMemberPriceModel::class.java)
        mBinding.activityBar.ivBack.setOnClickListener { finish() }
        mBinding.activityBar.titleView.text = "选择调价会员"
        members = intent?.extras?.getStringArrayList("list")
        receiverLiveData()

        adapter = SelectMembarPriceAdapter(data)
        mBinding.rv.adapter = adapter
        initEdit()
        mBinding.ivAll.setOnClickListener(this)
        mBinding.tvSearch.setOnClickListener(this)
        mBinding.tvQueDing.setOnClickListener(this)
        // 类型 0个人 1企业 2团队
        comType = MemberConfig.getInstance().comType.toString()
        viewModel.getMemberList(comType, keyWord)

    }

    private fun receiverLiveData() {
        viewModel.isLoadLiveData.observe(this, { t ->
            if (t) {
                DialogHelper.showProgressDialog(this, "数据获取中...")
                        .setCanceledOnTouchOutside(false)
            } else DialogHelper.dismissProgressDialog()
        })
        viewModel.msgLiveData.observe(this, { t ->
            MToast.showLongToast(this, t)
        })
        viewModel.dataListLiveData.observe(this, { t ->
            data.clear()
            data.addAll(t)
            members?.let {
                for (d in data) {
                    for (t in it) {
                        if (d.businessNo == t) {
                            d.isSelect = true
                        }
                    }
                }
            }
            adapter.notifyDataSetChanged()
        })
    }

    fun initEdit() {
        mBinding.etSearch.setOnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                keyWord = mBinding.etSearch.text.toString().trim()
                viewModel.getMemberList(comType, keyWord)
            }
            hideSoftInputFromWindow()
            false
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tv_search -> {
                keyWord = mBinding.etSearch.text.toString().trim()
                viewModel.getMemberList(comType, keyWord)
            }
            R.id.tv_que_ding -> {
                val list: ArrayList<String> = ArrayList<String>()
                for (d in data) {
                    if (d.isSelect) {
                        list.add(d.businessNo)
                    }
                }
                val intent = Intent().apply {
                    putStringArrayListExtra("list", list)
                }
                setResult(RESULT_OK, intent)
                finish()
            }
            R.id.iv_all -> {
                isAll = !isAll
                for (d in data) {
                    d.isSelect = isAll
                }

                mBinding.ivAll.run {
                    if (isAll) {
                        setImageResource(R.mipmap.btn_checed)
                    } else {
                        setImageResource(R.mipmap.btn_uncheced)
                    }
                }
                adapter.notifyDataSetChanged()
            }
        }
    }

    /**隐藏软禁盘**/
    fun hideSoftInputFromWindow() {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }

}